package resilienceManager.qmul.gemom;



import inl.resilienceManager.qmul.gemom.iniFrame;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultCaret;




import mo.qmul.gemom.ConnectionControl;
import no.geosoft.cc.geometry.Geometry;
import no.geosoft.cc.graphics.*;

/**
 * Scenario Frame
 * 
 * Load the Scenario
 * 
 * Simulated the Scenario
 * 
 * Main Frame when start
 *  
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */

public class ScenarioManager extends JFrame {

	GWindow window ;

	GScene  scene_;

	JTextArea textField = new JTextArea();

	JScrollPane scrollPane = new JScrollPane();

	private File file=null;

	private Scenario sc=new Scenario(this);


	public ScenarioManager() {

		setTitle("Re-allocation Test Harness");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


		//set the menu bar
		MenuBar mb=new MenuBar();
		setMenuBar(mb); 

		// set the File menu
		Menu fm= new Menu("File");
		mb.add(fm);
		MenuItem mi;

		// open menu item
		fm.add(mi = new MenuItem("Open", new MenuShortcut('O')));
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ScenarioSetup();
				sc.draw();
				textField.append("Load the Scenario and Configuration \n");
			}
		}
		);
		
		
		// open menu item
		fm.add(mi = new MenuItem("Connect", new MenuShortcut('T')));
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				 ConAction();
				
				
			}
		}
		);
		

		// close menu item
		fm.add(mi = new MenuItem("Exit", new MenuShortcut('X')));
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		}
		);

		// The Scenario Menu...
		Menu sm = new Menu("Scenario");
		mb.add(sm);

		// start menu item
		sm.add(mi = new MenuItem("Start", new MenuShortcut('S')));
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ScenarioSetup();
				sc.draw();
				textField.append("Load the Scenario and Configuration \n");
			}
		}
		);

		// Pause menu item
		sm.add(mi = new MenuItem("Pause", new MenuShortcut('P')));
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pauseAction();

			}
		}
		);

		// Resume menu item
		sm.add(mi = new MenuItem("Resume", new MenuShortcut('R')));
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resumeAction();
			}
		}
		);


		// The About Menu..
		Menu am = new Menu("Help");
		mb.add(am);
		am.add(mi = new MenuItem("About"));
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			}
		}
		);


		am.add(mi = new MenuItem("Credit"));
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			}
		}
		);




		//textField.setEditable(false);
		scrollPane.getViewport().add(textField);

		// always update the text field view point
		DefaultCaret caret = (DefaultCaret)textField.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

		scrollPane.setPreferredSize(new Dimension(500, 100));
		getContentPane().add("North", scrollPane);

		//set the main draw panel
		Container contentPane = getContentPane();
		window = new GWindow();
		contentPane.add(window.getCanvas(), "Center");
		scene_ = new GScene (window, "Scene");

		double w0[] = {0.0,    1200.0, 0.0};
		double w1[] = {1200.0, 1200.0, 0.0};
		double w2[] = {0.0,       0.0, 0.0};    
		scene_.setWorldExtent (w0, w1, w2);

		GStyle style = new GStyle();
		style.setForegroundColor (new Color (0, 0, 0));
		style.setBackgroundColor (new Color (255, 255, 255));
		style.setFont (new Font ("Dialog", Font.BOLD, 14));
		scene_.setStyle (style);


		JPanel p = new JPanel(new GridLayout(2, 2));

		addButton(p, "Scenario", new ActionListener() {
			public void actionPerformed(ActionEvent evt) {

				ScenarioSetup();
				sc.draw();
				textField.append("Load the Scenario and Configuration \n");

			}
		});

		addButton(p, "Start", new ActionListener() {
			public void actionPerformed(ActionEvent evt) {

				startAction();

			}
		});

		addButton(p, "Pause", new ActionListener() {
			public void actionPerformed(ActionEvent evt) {


				pauseAction();
						
				Random r = new Random();
				int gid=r.nextInt(sc.gn);
				while(gid<=0)
				{
					gid=r.nextInt(sc.gn);
				}
				AutoFrame af= new AutoFrame(sc.glist,gid);
				
				
				
				textField.append("Gbroker"+gid+" is broken down\n");

			}
		});

		addButton(p, "Resume", new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				resumeAction();

			}
		});

		contentPane.add(p, "South");

		setSize(500, 700);
		Dimension scr = Toolkit.getDefaultToolkit().getScreenSize(); 
		Dimension frm = this.getSize(); 
		setLocation( (scr.width - frm.width) / 2, 
				(scr.height - frm.height) / 2 - 18); 

	}

	public void addButton(Container c, String title, ActionListener a) {
		JButton b = new JButton(title);

		c.add(b);
		b.addActionListener(a);
	}

	/*
	 *
	 *
	 **/

	public boolean ScenarioSetup()
	{
		JFileChooser fc=new JFileChooser();

		XMLfilter filter = new XMLfilter();

		fc.setFileFilter(filter);

		int returnVal = fc.showOpenDialog(this);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			// get the scenario file
			file = fc.getSelectedFile();
			sc.setFile(file);
			return true; 
		}

		return false;

	}

	private void startAction()
	{
		sc.start();
		textField.append("Scenario Start \n");
	}

	public void pauseAction()
	{
		// pause the scenario
		try{
			sc.suspend();
			textField.append("Scenario Pause\n");
		}catch(Exception e)
		{
			e.printStackTrace();
		}

	}

	private void resumeAction()
	{
		//resume the scenario
		try{
			sc.resume();
			textField.append("Scenario Resume \n");
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	public void ConAction(){
		
		ConnectionControl cc= new ConnectionControl(this);
		cc.setVisible(true);
	}
	
	/**
	 * receive the text from other Frame and print in the textArea
	 * 
	 * 
	 * **/
	
	
	public void respondText(String s)
	{
		textField.append(s+"\n");
	}
	
	
	/**
	 * receive the message from connection control ( SMT)
	 * 
	 * @param gid: the ID of the broken gbroker
	 * 
	 * */
	public void respondBroker(int gid)
	{
		
		AutoFrame af= new AutoFrame(sc.glist,gid);
		textField.append("Alert: Gbroker-"+gid+" broken"+"\n");
		
	}
	
	/**
	 * 
	 * 
	 * 
	 * **/




}
